package org.springblade.modules.repair.controller;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.common.config.ProjectApiConfiguration;
import org.springblade.core.launch.constant.TokenConstant;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.WebUtil;
import org.springblade.modules.repair.dto.video.SwitchCallDTO;
import org.springblade.modules.repair.dto.video.TimingVideoCallDTO;
import org.springblade.modules.repair.dto.video.TokenDTO;
import org.springblade.modules.repair.vo.video.CloseCallVO;
import org.springblade.modules.repair.vo.video.OpenStatusVO;
import org.springframework.web.bind.annotation.*;

/**
 * @Description 梯联网视频
 * @Author ysq
 * @Date 2022/10/24
 **/
@AllArgsConstructor
@RestController
@RequestMapping("/tlw/video")
@Api(value = "梯联网视频接口", tags = "梯联网视频接口")
public class TlwVideoController {

	private final ProjectApiConfiguration projectApiConfiguration;
	private final String TLW_VIDEO_PREFIX = "/tlw/video/v1/devices/";

	/**
	 * 获取视频通话信息
	 */
	@ApiOperation(value = "获取视频通话信息")
	@PostMapping("/v1/devices/videoCall")
	public JSONObject order(@RequestBody TokenDTO tokenDTO) {
		String result = HttpRequest.post(projectApiConfiguration.getEstate() + TLW_VIDEO_PREFIX + "videoCall")
			.header(TokenConstant.HEADER, AuthUtil.getHeader(WebUtil.getRequest()))
			.body(JSONUtil.toJsonStr(tokenDTO)).execute().body();
		return JSONUtil.parseObj(result);
	}

	/**
	 * 定时请求推送
	 */
	@ApiOperation(value = "定时请求推送")
	@PostMapping("/v1/devices/timing/videoCall")
	public JSONObject timingVideoCall(@RequestBody TimingVideoCallDTO videoCallDTO) {
		String result = HttpRequest.post(projectApiConfiguration.getEstate() + TLW_VIDEO_PREFIX + "timing/videoCall")
			.header(TokenConstant.HEADER, AuthUtil.getHeader(WebUtil.getRequest()))
			.body(JSONUtil.toJsonStr(videoCallDTO)).execute().body();
		return JSONUtil.parseObj(result);
	}

	/**
	 * 切换视频通话模式
	 */
	@ApiOperation(value = "切换视频通话模式")
	@PostMapping("/v1/devices/switch/videoCall")
	public JSONObject switchVideoCall(@RequestBody SwitchCallDTO switchCallDTO) {
		String result = HttpRequest.post(projectApiConfiguration.getEstate() + TLW_VIDEO_PREFIX + "switch/videoCall")
			.header(TokenConstant.HEADER, AuthUtil.getHeader(WebUtil.getRequest()))
			.body(JSONUtil.toJsonStr(switchCallDTO)).execute().body();
		return JSONUtil.parseObj(result);
	}


	@PostMapping("/v1/devices/open/status")
	@ApiOperation(value = "开启实时数据推送")
	public JSONObject openStatus(@RequestBody OpenStatusVO openStatusVO) {
		String result = HttpRequest.post(projectApiConfiguration.getEstate() + TLW_VIDEO_PREFIX + "open/status")
			.header(TokenConstant.HEADER, AuthUtil.getHeader(WebUtil.getRequest()))
			.body(JSONUtil.toJsonStr(openStatusVO)).execute().body();
		return JSONUtil.parseObj(result);
	}

	@PostMapping("/v1/devices/close/videoCall")
	@ApiOperation(value = "挂断视频模式")
	public JSONObject closeVideoCall(@RequestBody CloseCallVO closeCallVO) {
		String result = HttpRequest.post(projectApiConfiguration.getEstate() + TLW_VIDEO_PREFIX + "close/videoCall")
			.header(TokenConstant.HEADER, AuthUtil.getHeader(WebUtil.getRequest()))
			.body(JSONUtil.toJsonStr(closeCallVO)).execute().body();
		return JSONUtil.parseObj(result);
	}

	@GetMapping("/v2/devices/videoCall/detail/{id}")
	@ApiOperation(value = "获取视频通话相关信息")
	public JSONObject getVideoCallDetail(@PathVariable("id") String id) {
		String result = HttpRequest.get(projectApiConfiguration.getEstate() + "/tlw/video/v2/devices/videoCall/detail/" + id)
			.header(TokenConstant.HEADER, AuthUtil.getHeader(WebUtil.getRequest())).execute().body();
		return JSONUtil.parseObj(result);
	}
}
